package com.yzy.soft.config;

import com.alibaba.druid.pool.DruidDataSource;
//import com.baomidou.mybatisplus.plugins.PaginationInterceptor;
//import com.sinosoft.liscw.common.constant.DSEnum;
import com.baomidou.mybatisplus.entity.GlobalConfiguration;
import com.baomidou.mybatisplus.mapper.ISqlInjector;
import com.baomidou.mybatisplus.mapper.LogicSqlInjector;
import com.yzy.soft.config.properties.DruidProperties;
//import com.sinosoft.liscw.core.datascope.DataScopeInterceptor;
//import com.sinosoft.liscw.core.mutidatasource.DynamicDataSource;
//import com.sinosoft.liscw.core.mutidatasource.config.MutiDataSourceProperties;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.transaction.annotation.EnableTransactionManagement;

/**
 * MybatisPlus配置
 *
 * @author stylefeng
 * @Date 2017/5/20 21:58
 */
@Configuration
@EnableTransactionManagement(order = 1)//由于引入多数据源，所以让spring事务的aop要在多数据源切换aop的后面 "com.sinosoft.liscw.system.dao",
@MapperScan(basePackages = {"com.yzy.soft.common.persistence.dao", "com.yzy.soft.common.persistence.dao.mapping"})
public class MybatisPlusConfig {

    @Autowired
    DruidProperties druidProperties;

//    @Autowired
//    MutiDataSourceProperties mutiDataSourceProperties;


    /**
     * 另一个数据源
     */
//    private DruidDataSource bizDataSource() {
//        DruidDataSource dataSource = new DruidDataSource();
//        druidProperties.config(dataSource);
////        mutiDataSourceProperties.config(dataSource);
//        return dataSource;
//    }
//
    /**
     * guns的数据源
     */
    @Bean
    @Primary
    public DruidDataSource dataSourceLiscw(){
        DruidDataSource dataSource = new DruidDataSource();
        druidProperties.config(dataSource);
        return dataSource;
    }
//
    /**
     * 单数据源连接池配置
     */
    @Bean
//    @ConditionalOnProperty(prefix = "liscw", name = "muti-datasource-open", havingValue = "false")
    public DruidDataSource singleDatasource() {
        return dataSourceLiscw();
    }

    /**
     * 多数据源连接池配置
     */
//    @Bean
//    @ConditionalOnProperty(prefix = "guns", name = "muti-datasource-open", havingValue = "true")
//    public DynamicDataSource mutiDataSource() {
//
//        DruidDataSource dataSourceGuns = dataSourceGuns();
//        DruidDataSource bizDataSource = bizDataSource();
//
//        try {
//            dataSourceGuns.init();
//            bizDataSource.init();
//        }catch (SQLException sql){
//            sql.printStackTrace();
//        }
//
//        DynamicDataSource dynamicDataSource = new DynamicDataSource();
//        HashMap<Object, Object> hashMap = new HashMap();
//        hashMap.put(DSEnum.DATA_SOURCE_GUNS, dataSourceGuns);
//        hashMap.put(DSEnum.DATA_SOURCE_BIZ, bizDataSource);
//        dynamicDataSource.setTargetDataSources(hashMap);
//        dynamicDataSource.setDefaultTargetDataSource(dataSourceGuns);
//        return dynamicDataSource;
//    }

    /**
     * mybatis-plus分页插件
     */
//    @Bean
//    public PaginationInterceptor paginationInterceptor() {
//        return new PaginationInterceptor();
//    }

    /**
     * 数据范围mybatis插件
     */
//    @Bean
//    public DataScopeInterceptor dataScopeInterceptor() {
//        return new DataScopeInterceptor();
//    }
    @Bean
    public GlobalConfiguration globalConfiguration() {
        GlobalConfiguration conf = new GlobalConfiguration(sqlInjector());
        conf.setLogicDeleteValue("1");
        conf.setLogicNotDeleteValue("0");
        conf.setIdType(3);
        return conf;
    }
    /**
     * 注入sql注入器
     */
    @Bean
    public ISqlInjector sqlInjector(){
        return new LogicSqlInjector();
    }
}
